const regexp = require("../../utils");

/**
 * 获取表单字段模板（基础相关）
 * @param {common|query} type 表单类型
 * @returns
 */
module.exports = (type = "common") => {
  const isQuery = type === "query";
  return {
    /***** 文本类 *****/
    // id
    T_Id: {
      prop: "id",
      label: "ID",
      attrs: {
        maxlength: 10,
      },
    },
    // 备注
    T_Remark: {
      // prop: "remark", // 省略不写，则和键名保持一致
      label: "备注",
      type: "input",
      attrs: {
        type: isQuery ? undefined : "textarea",
        maxlength: 50,
      },
    },
    /***** 日期时间类 *****/
    // 创建时间
    T_CreateTime: {
      prop: "create_time",
      label: "创建时间",
      type: "date-picker",
      attrs: {
        type: isQuery ? undefined : "datetime",
      },
    },
    /***** 表单校验类 *****/
    //电话号码
    T_Phone: {
      // prop: "phone", // 省略不写，则和键名保持一致
      label: "电话",
      rules: isQuery ? undefined : [{ pattern: regexp.phone, message: "请输入正确的11位电话号码", trigger: "change" }],
      attrs: {
        maxlength: 11,
      },
    },
    //密码
    T_Password: {
      label: "密码",
      ...(isQuery
        ? { attrs: { maxlength: 16 } }
        : {
            rules: [
              { min: 6, message: "密码长度不能小于6位", trigger: "change" },
              { pattern: regexp.password, message: "请输入正确的6~16位字母 + 数字组合密码", trigger: "change" },
            ],
            attrs: {
              type: "password",
              maxlength: 16,
              showPassword: true,
            },
          }),
    },
    //15~18位身份证号
    T_Identity: {
      label: "身份证号",
      rules: isQuery
        ? undefined
        : [
            { min: 15, message: "身份证号长度不能小于15位", trigger: "change" },
            { pattern: regexp.identity, message: "请输入正确的15~18位身份证号", trigger: "change" },
          ],
      attrs: {
        maxlength: 18,
      },
    },
    //邮箱
    T_Email: {
      label: "邮箱",
      rules: isQuery ? undefined : [{ pattern: regexp.email, message: "请输入正确的邮箱地址", trigger: "change" }],
    },
    /***** 数值类 *****/
    //年龄
    T_Age: {
      label: "年龄",
      ...(isQuery
        ? { type: "BaseNumberRange", attrs: { min: 0, max: 150 } }
        : {
            type: "input-number",
            attrs: { min: 0, max: 150, slots: { suffix: "岁" } },
          }),
    },
    // 数量
    T_Amount: {
      label: "数量",
      ...(isQuery
        ? { type: "BaseNumberRange", attrs: { min: 0, max: 100 } }
        : { type: "input-number", attrs: { min: 0, max: 100, slots: { suffix: "个" } } }),
    },
    // 人民币：最小值为0，保留两位小数
    T_Price: {
      prop: "price",
      label: "价格",
      ...(isQuery
        ? { type: "BaseNumberRange", attrs: { min: 0, max: 99999, precision: 2 } }
        : {
            type: "input-number",
            attrs: {
              min: 0,
              max: 99999,
              precision: 2,
              step: 0.1,
              controlsPosition: "right",
              slots: {
                suffix: "￥", // 单位：元、￥
              },
            },
          }),
    },
    /***** 开关类 *****/
    // 是否开关
    T_YesNoStatus: {
      prop: "is",
      label: "是否",
      ...(isQuery
        ? { type: "select", attrs: { options: "D_YesNoStatus" } }
        : {
            type: "switch",
            attrs: { activeText: "是", inactiveText: "否" },
          }),
    },
    // 是否禁用下拉项
    T_EnableStatus: {
      prop: "status",
      label: "启用状态",
      ...(isQuery
        ? {
            type: "select",
            attrs: { options: "D_EnableStatus" },
          }
        : {
            type: "switch",
            attrs: { activeText: "启用", inactiveText: "禁用" },
          }),
    },
  };
};
